package cn.gxa.j229.bug.finance.service.loanuser.impl;

/*
 *
 *@author leijun
 *
 *@creating 2020/9/27 10:10
 *
 */

import cn.gxa.j229.bug.finance.mapper.CheckMapper;
import cn.gxa.j229.bug.finance.mapper.PermissionMapper;
import cn.gxa.j229.bug.finance.pojo.Check;
import cn.gxa.j229.bug.finance.pojo.Permission;
import cn.gxa.j229.bug.finance.service.loanuser.CheckService;
import cn.gxa.j229.bug.finance.utils.JsonResult;
import cn.gxa.j229.bug.finance.utils.MD5Util;
import cn.gxa.j229.bug.finance.utils.Salt;
import cn.gxa.j229.bug.finance.utils.TreeUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.List;
import java.util.Map;

/**
 * @ClassName CheckServiceImpl
 * @Description TODO
 * @Author 俊
 * @Date 2020/9/27 10:10
 * @Version 1.0
 */
@Service("checkService")
public class CheckServiceImpl implements CheckService {

    @Resource
    CheckMapper checkMapper;

    @Resource
    PermissionMapper permissionMapper;

    @Override
    public int insertSelective(Check check) {
        try {
            Map<String, String> checkUser = checkMapper.findByUsername(check.getUsername());
            if (checkUser!=null){
                return 0;
            }
            if (check.getPassword()==null || check.getPassword().trim().equals("")){
                return 0;
            }
            String salt = Salt.createSalt();
            String password = check.getPassword();
            check.setPassword(MD5Util.encode(password+salt));
            check.setSalt(salt);
        }catch (Exception e){
            e.printStackTrace();
        }
        return checkMapper.insertSelective(check);
    }

    @Override
    public List<Permission> login(Check check) {
        try{
            Map<String, String> checkUser = checkMapper.findByUsername(check.getUsername());

            if (checkUser.get("password").equals(MD5Util.encode(check.getPassword()+checkUser.get("salt")))){
                check.setPassword(MD5Util.encode(check.getPassword()+checkUser.get("salt")));
                Integer id  = checkMapper.findByUsernameAndPassword(check);
                List<Permission> all = permissionMapper.findAll(id);

                List<Permission> treeList = TreeUtils.getTreeList(all);

                return treeList;
            }
        }catch (Exception e){
           e.printStackTrace();
        }
        return null;
    }

    @Override
    public Map<String, Object> findTest(Check check) {
        return checkMapper.findByUsername2(check.getUsername());
    }

    @Override
    public Check findByEmail(String email) {
        if (email==null || email.trim().equals("")){
            return null;
        }
        return checkMapper.findByEmail(email);
    }

    @Override
    public int updatePassword(Check check) {
        check.setPassword(MD5Util.encode(check.getPassword()+check.getSalt()));
        return checkMapper.updatePassword(check);
    }

    @Override
    public Check findByUsername(String username) {
        if (username==null||username.trim().equals("")){
            return null;
        }
        return checkMapper.findByUsername3(username);
    }
}
